"""
SA(Simulated Annealing)
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sko.SA import SA

plt.style.use(['bmh'])


def demo_func(x):
    x1, x2, x3 = x
    return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2


sa = SA(func=demo_func, x0=[1, 1, 1])
x_star, y_star = sa.run()

plt.figure(figsize=(10, 6), dpi=128)
plt.plot(range(len(sa.best_y_history)), sa.best_y_history, 'b-', label='y')
plt.plot(range(len(sa.best_x_history)), np.array(
    sa.best_x_history)[:, 0], 'r-', label='x1')
plt.plot(range(len(sa.best_x_history)), np.array(
    sa.best_x_history)[:, 1], 'g-', label='x2')
plt.plot(range(len(sa.best_x_history)), np.array(
    sa.best_x_history)[:, 2], 'y-', label='x3')
plt.title("SA(Simulated Annealing)")
plt.xlabel("iter")
plt.ylabel("var")
plt.legend(loc='upper right')
plt.show()
plt.savefig('第12章：现代优化算法/SA')
